package io.zhengsh.nio.datagramchannel;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

public class DatagramChannelServer {

	public static void main(String[] args) throws IOException {
		DatagramChannel server = DatagramChannel.open();
		server.socket().bind(new InetSocketAddress(25000));

		ByteBuffer receiveBuffer = ByteBuffer.allocate(1024);
		while (true) {
			receiveBuffer.clear();
			SocketAddress receiveAddress = server.receive(receiveBuffer);
			receiveBuffer.flip();
			System.out.println(receiveAddress.toString());
			System.out.println(StandardCharsets.UTF_8.decode(receiveBuffer));
		}
	}
}
